<?php

namespace App\Modules\Wechat\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use EasyWeChat\Message\Text;
use EasyWeChat\Foundation\Application;
use DB;
use Cache;
class WechatController extends Controller
{
    /**
     * 被动消息消息回复
     * @return mixed
     */
    public function index(Application $wechat)
    {
        $wechat->server->setMessageHandler(function($message){
            if($message->Content == "股票"){
                return $this->getStockInfo();
            }else{
                return $message->Content;
            }
        });
        return $wechat->server->serve();
    }

    /**
     * 消息验证
     * @return mixed
     */
    public function sendMsg(Application $wechat)
    {
        if($str = $this->getStockInfo()){
            $tmp = env('WECHAT_USER','oJeeOwIr9rf0zq40kXtrChP4gyhs,');
            $lists = explode(',',$tmp);
            array_pop($lists);
            for ($i = 0; $i < count($lists); $i++ ){
                $message = new Text(['content' => $str]);
                $result = $wechat->staff->message($message)->to($lists[$i])->send();
            }
            return $result;
        }
    }


    public function getStockInfo(){
        $arr = [28344,26926,56533,64258];
        $response = [];
        for ($i=0;$i<count($arr);$i++){
            $lists = DB::connection('stock')->table('users_position')->where('uid',$arr[$i])->where('is_position', 1)->get();
            foreach ($lists as $v){
                $tmp = DB::connection('stock')->table('transaction as t')
                    ->join('users as u', 't.uid', '=', 'u.uid')
                    ->join('users_funds as uf', 't.uid', '=', 'uf.uid')
                    ->where('t.pid',$v->id)
                    ->where('t.stock',$v->stock)
                    ->where('t.status', 1)
                    ->orderBy('t.id','desc')
                    ->select('t.id','t.stock','t.stock_name','t.price','t.time','t.number', 't.fee','u.username','t.type','uf.funds')
                    ->first();
                if(Cache::has($tmp->id)){
                    $id = Cache::get($tmp->id)->id;
                    if($id != $tmp->id){
                        Cache::forget($tmp->id);
                        Cache::put($tmp->id,$tmp,720);
                        $tmp->bili = round(($tmp->price * $tmp->number + $tmp->fee)/$tmp->funds*100,2);
                        $response[] = $tmp;
                    }
                }else{
                    Cache::put($tmp->id,$tmp,720);
                    $t['username'] = $tmp->username;
                    $t['stock'] = $tmp->stock;
                    $t['stock_name'] = $tmp->stock_name;
                    $t['price'] = $tmp->price;
                    $t['number'] = $tmp->number;
                    $t['type'] = $tmp->type == 1? '买' : '卖';
                    $t['time'] = $tmp->time;
                    $t['bili'] = round(($tmp->price * $tmp->number + $tmp->fee)/$tmp->funds*100,2).'%';
                    $response[] = join('|', $t);
                }
            }
        }
        $response = join("\n",$response);
        return $response;
    }
}
